package edu.neusoft.android.yuejian14.model;

import java.util.List;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.javabean.Club;
import edu.neusoft.android.yuejian14.service.RetrofitService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static edu.neusoft.android.yuejian14.Const_Service.BASE_URL;

public class ClubModel {
    private Retrofit retrofit;
    public ClubModel(){
        //创建实例
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //俱乐部列表
    public void getclublist(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<Club>> call = retrofitService.getclublist();
        Callback<List<Club>> callback = new Callback<List<Club>>() {
            @Override
            public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
                List<Club> club= response.body();
                listener.onSuccess(club,1);
            }
            @Override
            public void onFailure(Call<List<Club>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //俱乐部详情
    public void getClubById(String clubid, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call = retrofitService.getClubById(clubid);
        Callback<Club> callback = new Callback<Club>() {
            @Override
            public void onResponse(Call<Club> call, Response<Club> response) {
                Club club = response.body();
                listener.onSuccess(club,1);
            }

            @Override
            public void onFailure(Call<Club> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //搜索俱乐部
    public void getClubBySearch(String clubName, final RetrofitListener listener) {
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<Club>> call = retrofitService.getClubBySearch(clubName);
        Callback<List<Club>> callBack = new Callback<List<Club>>() {
            @Override
            public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
                List<Club> club = response.body();
                listener.onSuccess(club,1);

            }
            @Override
            public void onFailure(Call<List<Club>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callBack);
    }

}
